﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using TCPSocket_DLL;

namespace Client {
    public partial class Client : Form {
        Stopwatch mSP;
        TcpSocket mControlSocket;
        TcpSocket mDataSocket;
        byte mCode;
        public Client() {
            mCode = 0;
            InitializeComponent();
            controlPort.SelectedIndex = 0;
            dataPort.SelectedIndex = 0;
            mSP = new Stopwatch();
        }

        private void ButtonConnectClick(object sender, EventArgs e) {
            mControlSocket = new TcpSocket(addressTextBox.Text, Convert.ToInt16(controlPort.SelectedItem.ToString()), DataStreamType.STRING);
            mControlSocket.Connect(addressTextBox.Text, Convert.ToInt16(controlPort.SelectedItem.ToString()), true);
            mControlSocket.DataReceived += ControlDataReceived;

            mDataSocket = new TcpSocket(addressTextBox.Text, Convert.ToInt16(dataPort.SelectedItem.ToString()), DataStreamType.BITMAP);
            //dataSocket.Connect(addressTextBox.Text, Convert.ToInt16(dataPort.SelectedItem.ToString()), true);
            mDataSocket.DataReceived += DataReceived;
            mDataSocket.StartServer();
            controller.Changed += SendControlInfo;
        }

        void SendControlInfo(object direction, EventArgs e) {
            mCode = 111;
            int[] tmp = direction as int[];
            if (tmp == null) throw new ArgumentNullException("tmp");
            char[] sendBuffer = new char[4];
            sendBuffer[0] = Convert.ToChar(mCode);
            sendBuffer[1] = Convert.ToChar(tmp[0]); //Left Motor PWM
            sendBuffer[2] = Convert.ToChar(tmp[1]); //Right Motor PWM
            sendBuffer[3] = Convert.ToChar((mCode + tmp[0] + tmp[1]) % 53);
            mSP.Start();
            mControlSocket.SendString(new string(sendBuffer));
        }
        private void DataReceived(object bMap, EventArgs e) {
            Invoke(new MethodInvoker(delegate {
                pictureBox1.Image = ((Bitmap)bMap);
            }));
        }
        private void ControlDataReceived(object a, EventArgs e) {
            mSP.Stop();
            if (mSP.ElapsedMilliseconds > 0)
                toolStripStatusLatencyTime.Text = "" + mSP.ElapsedMilliseconds;
            mSP.Reset();
            Invoke(new MethodInvoker(delegate {
                byte[] tmp = a as byte[];
                if (tmp == null) throw new ArgumentNullException("tmp");
                try {
                    for (int i = 0; i < tmp.Length; i++) {
                        if (tmp[i] == 0) break;
                        if (tmp[i] > 32)
                            logTextBox.Text += (char)tmp[i];
                        else
                            if ((tmp[i] == 10 && tmp[i + 1] == 13) || (tmp[i] == 13 && tmp[i + 1] == 10)) {
                                logTextBox.Text += " \r\n ";
                                i++;
                            } else {
                                if (tmp[i] == 32 && tmp[i + 1] == 32 && tmp[i + 2] == 32 && tmp[i + 3] == 32) {
                                    logTextBox.Text = "";
                                    i += 3;
                                }
                            }
                    }
                } catch (IndexOutOfRangeException){//prevent out of index 
                }
            }));
        }
        private void ClientKeyPress(object sender, KeyPressEventArgs e) {
            mControlSocket.SendString(e.KeyChar.ToString());
        }
        private void ClientFormClosing(object sender, FormClosingEventArgs e) {
            if (mControlSocket != null) {
                mControlSocket.SendString("SSSS");
                mControlSocket.Stop();
            }
            if (mDataSocket != null)
                mDataSocket.Stop();
            controller.StopJoy();
        }

        private void Button1Click(object sender, EventArgs e) {
            controller.SetMFrom(this);
        }

        private void ClearLogButtonClick(object sender, EventArgs e) {
            logTextBox.Text = "";
        }
    }
}
